const express = require('express');
// https://www.npmjs.com/package/http-proxy-middleware
const { createProxyMiddleware: proxy } = require('http-proxy-middleware');

const ctx = '/tikeyc';

const proxyOption = {
  target: 'http://172.16.4.51:8483/tikeyc',
  changeOrigoin: true,
  ws: true,
  pathRewrite: { [`^${ctx}/api`]: '' }
};

const webLoadImage = {
  target: 'http://www.xxx.com/xx_picture',
  changeOrigin: true,
  pathRewrite: { [`^${ctx}/api/web/loadImage/image`]: '' }
};

const telecomLoadImage = {
  target: 'http://www.xxx.com/xx_picture',
  changeOrigin: true,
  pathRewrite: { [`^${ctx}/api/telecom/loadImage/image`]: '' }
};

const app = express();
// html导出图片。跨域问题处理（图片链接服务不支持html2canvas属性CORS的时候）
app.use(`${ctx}/api/web/loadImage/image`, proxy(webLoadImage));

app.use(`${ctx}/api/telecom/loadImage/image`, proxy(telecomLoadImage));

app.use(`${ctx}/api`, proxy(proxyOption));

app.use(ctx, express.static('dist'));

app.listen(1024, () => console.log(`${ctx} listening on port ${port}!`));
